package com.itheima.servlet;

import com.itheima.service.StuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    StuService stuService = new StuService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取浏览器端传过来的用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //判断用户名是否重复
        boolean opFlag = stuService.checkExists(username);
        //调用service,将用户名和密码保存到数据库
        //返回一个操作结果
        //boolean opFlag = stuService.register(username,password);

        //给浏览器响应
        resp.setContentType("text/html;charset=utf-8");
        if (opFlag){
            resp.getWriter().write("注册成功:"+ username);
        }else {
            resp.getWriter().write("用户密码错误,请重新登录:"
                    +"<a href='/HM/login.html'>重新登陆</a>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
